package cn.exrick.xboot.shop.controller;

import cn.exrick.xboot.core.common.utils.ResultUtil;
import cn.exrick.xboot.core.common.utils.SecurityUtil;
import cn.exrick.xboot.core.common.vo.Result;
import cn.exrick.xboot.core.entity.Member;
import cn.exrick.xboot.shop.entity.ShopOrder;
import cn.exrick.xboot.shop.entity.vo.OrderVo;
import cn.exrick.xboot.shop.service.IShopOrderService;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
* <p>
    * 客户预约  前端控制器
    * </p>
*
* @author yuhaifei
* @since 2022-01-26
*/
@RestController
@RequestMapping("/xboot/shoporder")
public class ShopOrderController {


    @Autowired
    private IShopOrderService shopOrderService;

    @Autowired
    private SecurityUtil securityUtil;


    /**
     * 获取预约内容
     * @param ymonth 年月  YYYY-MM
     * @return
     */
    @GetMapping("getOrderList")
    public Result getOrderList(String ymonth){
        List<OrderVo> list = shopOrderService.getOrderList(ymonth);
        return ResultUtil.data(list);
    }

    /**
     * 添加预约
     * @param order
     * @return
     */
    @PostMapping("addOrder")
    public Result addOrder(ShopOrder order){
        // 后续加个锁防止重复同时提交冲突
        //////////////////
        // 确认时间是否可预约
        Date startTime = order.getStartTime();
        Date endTime = DateUtil.offset(startTime, DateField.HOUR, 2);
        QueryWrapper<ShopOrder> wrapper = new QueryWrapper<ShopOrder>();
        wrapper.between("start_time", startTime, endTime)
                .or()
                .between("end_time", startTime, endTime);
        wrapper.ne("confirm_status", 2);
        int count = shopOrderService.count(wrapper);
        if(count > 0){
            return ResultUtil.error("该时间段已有预约");
        }

        // 插入预约
        Member m = securityUtil.getCurrMember();
        order.setTitle(m.getNickname() + "的预约");
        order.setEndTime(endTime);
        order.setAvatar(m.getAvatar());
        order.setMobile(m.getMobile());
        order.setUserId(m.getId());
        order.setNickname(m.getNickname());
        order.setOpenId(m.getOpenId());
        order.setCreateTime(new Date());
        order.setConfirmStatus(0);
        shopOrderService.save(order);
        return ResultUtil.data("预约成功");

    }

    /**
     * 获取所有预约
     * @return
     */
    @GetMapping("getAllOrders")
    public Result getAllOrders(){
        List<ShopOrder> list = shopOrderService.list();
        return ResultUtil.data(list);
    }

    


//    public static void main(String[] args) {
//        Date startTime = new Date();
//        Date endTime = DateUtil.offset(startTime, DateField.HOUR, 2);
//        System.out.println(startTime);
//        System.out.println(endTime);
//    }
}
